/*
 *  Copyright 2012 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.pc.ui.client.actions;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import org.opu.pc.ui.client.i18n.AbstractI18N;

import org.opu.pc.ui.client.i18n.PublicationI18N;
import org.opu.pc.ui.client.widgets.PropertiesEditPane;
import org.opu.pc.ui.client.widgets.PropertiesEditPane.PropertyOption;
import static org.opu.pc.ui.util.GUIUtils.*;
import static java.awt.BorderLayout.SOUTH;

/**
 * 
 * @author yura
 */
public abstract class AbstractDialogAction extends AbstractAction {

	private static final long serialVersionUID = 2116421697436149815L;
    protected AbstractI18N i18n = new PublicationI18N();

	public AbstractDialogAction() {
		// because it will show only dialog, actual action will be on submiting.
		super(true);
	}

	public AbstractDialogAction(String label) {
		// because it will show only dialog, actual action will be on submiting.
		super(true, label);
	}

	@Override
	public void action(ActionEvent e) throws Exception {
		JDialog dialog = mainView.getDialog();
		dialog.setTitle(getDialogTitle());

		LinkedHashMap<String, Object> properties = new LinkedHashMap<String, Object>();
		initProp(properties);

		Map<String, List<PropertyOption>> map = new HashMap<String, List<PropertyOption>>();

		initPropMap(map);

		final PropertiesEditPane pane = new PropertiesEditPane(properties,
				i18n, map);

		final OkAction okAction = getOkAction(dialog, pane);
		okAction.mainView = this.mainView;

		final AbstractAction cancelAction = getCancelAction(okAction);
		cancelAction.mainView = this.mainView;

		dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		dialog.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				cancelAction.actionPerformed(null);
			}
		});

		JPanel cp = new JPanel(new BorderLayout());
		addPropertiesPane(cp, pane);
		
		Box b = Box.createHorizontalBox();
		b.add(new JButton(okAction));
		b.add(Box.createHorizontalStrut(DEFAULT_SPACE));
		b.add(new JButton(cancelAction));
		cp.add(vBorder(hCenter(b), DEFAULT_SPACE_HALF, DEFAULT_SPACE_HALF), SOUTH);

		dialog.getContentPane().add(cp);

		dialog.pack();
		dialog.setSize(getMinimalDialogWidth(), getMinimalDialogHeight(dialog));
        dialog.setLocationRelativeTo(null);
        dialog.setVisible(true);
		onActionFinish();
	}

	protected void addPropertiesPane(JPanel cp, PropertiesEditPane pane) {
		cp.add(pane);
	}

	protected int getMinimalDialogWidth() {
		return 400;
	}

    protected int getMinimalDialogHeight(JDialog dialog) {
        int h = dialog.getSize().height;
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        if (h > d.height){
            h = d.height;
        }
        return h;
    }

	protected void initPropMap(Map<String, List<PropertyOption>> map) {
	}

	protected void initProp(LinkedHashMap<String, Object> properties) {
	}

    protected AbstractAction getCancelAction(final OkAction okAction) {
        return new AbstractAction() {

            private static final long serialVersionUID = 4738762316129123369L;

            @Override
            public String getName() {
                return getCacelActionName();
            }

            @Override
            public void runAction(ActionEvent e) throws Exception {
                okAction.dialog.dispose();
                okAction.dialog = null;
            }
        };
    }

	protected abstract OkAction getOkAction(JDialog dialog,
			final PropertiesEditPane pane);

	protected String getCacelActionName() {
		return "Cancel";
	}

	public static abstract class OkAction extends AbstractAction {
		protected final PropertiesEditPane pane;
		protected JDialog dialog;
		private static final long serialVersionUID = -6845124793534976057L;

		public OkAction(PropertiesEditPane pane, JDialog dialog) {
			this.pane = pane;
			this.dialog = dialog;
		}

		@Override
		public String getName() {
			return "Ok";
		}

        public void hideDialog(){
            if (dialog != null) {
				dialog.dispose();
				dialog = null;
			}
        }

		@Override
		public abstract void runAction(ActionEvent e) throws Exception;
	}

}
